package com.mojiayi.learn.sentinel.api.controller;

import com.mojiayi.learn.common.enums.ResponseStatusEnum;
import com.mojiayi.learn.common.response.CommunicationFactory;
import com.mojiayi.learn.common.response.CommunicationResponse;
import com.mojiayi.learn.common.result.ListResultSet;
import com.mojiayi.learn.common.result.ResultSet;
import com.mojiayi.learn.sentinel.stub.domain.dto.UserDetailDTO;
import com.mojiayi.learn.sentinel.stub.domain.params.UserDetailParams;
import com.mojiayi.learn.sentinel.stub.service.IUserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserProvider userProvider;

    @RequestMapping("/get-by-phone")
    public CommunicationResponse getUserDetailByPhone(String phone) {
        UserDetailParams userDetailParams = new UserDetailParams();
        userDetailParams.setPhone(phone);
        ResultSet<UserDetailDTO> resultSet = userProvider.getUserDetailByPhone(userDetailParams);
        CommunicationResponse response = CommunicationFactory.createCommunicationResponse();
        if (resultSet.isSucess()) {
            response.addSingleDataItem("userDetail", resultSet.getResult());
        } else {
            response.quickSet(ResponseStatusEnum.ILLEGAL_OPERATION);
        }
        return response;
    }

    @RequestMapping("/qry-by-real-name")
    public CommunicationResponse qryUserDetailByRealName(String realName) {
        UserDetailParams userDetailParams = new UserDetailParams();
        userDetailParams.setRealName(realName);
        ListResultSet<UserDetailDTO> listResultSet = userProvider.qryUserDetailByRealName(userDetailParams);
        CommunicationResponse response = CommunicationFactory.createCommunicationResponse();
        if (listResultSet.isSucess()) {
            response.addSingleDataItem("userDetailList", listResultSet.getResults());
        } else {
            response.quickSet(ResponseStatusEnum.ILLEGAL_OPERATION);
        }
        return response;
    }
}
